package com.mkvld.appietijdenv2;

import java.util.List;

import com.mkvld.appietijdenv2.data.Break;
import com.mkvld.appietijdenv2.data.Utils;

import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.NumberPicker;

public class FragBreaksV14 extends DialogFragment {
	NumberPicker fromHour, fromMin, toHour, toMin;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.frag_breaks, null, false);
		
		fromHour = (NumberPicker) v.findViewById(R.id.fb_from_hours);
		fromHour.setMinValue(0);
		fromHour.setMaxValue(23);
		fromHour.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
			
			@Override
			public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
				if (getTimeDiff() < 0) {
					toHour.setValue(fromHour.getValue());
					toMin.setValue(fromMin.getValue());
				}
				calculateBreaks();
			}
		});
		fromHour.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
		
		fromMin = (NumberPicker) v.findViewById(R.id.fb_from_min);
		fromMin.setMinValue(0);
		fromMin.setMaxValue(3);
		fromMin.setDisplayedValues(new String[]{"00", "15", "30", "45"});
		fromMin.setWrapSelectorWheel(true);
		fromMin.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
			
			@Override
			public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
				if (oldVal == 3 && newVal == 0)
					fromHour.setValue((fromHour.getValue() + 1) % 24);
				else if (oldVal == 0 && newVal == 3)
					fromHour.setValue((fromHour.getValue() + 23) % 24);
				if (getTimeDiff() < 0) {
					toHour.setValue(fromHour.getValue());
					toMin.setValue(fromMin.getValue());
				}
				calculateBreaks();
			}
		});
		fromMin.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
		
		toHour = (NumberPicker) v.findViewById(R.id.fb_to_hours);
		toHour.setMinValue(0);
		toHour.setMaxValue(23);
		toHour.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
			
			@Override
			public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
				if (getTimeDiff() < 0) {
					fromHour.setValue(toHour.getValue());
					fromMin.setValue(toMin.getValue());
				}
				calculateBreaks();
			}
		});
		toHour.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
		
		toMin = (NumberPicker) v.findViewById(R.id.fb_to_min);
		toMin.setMinValue(0);
		toMin.setMaxValue(3);
		toMin.setDisplayedValues(new String[]{"00", "15", "30", "45"});
		toMin.setWrapSelectorWheel(true);
		toMin.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
			
			@Override
			public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
				if (oldVal == 3 && newVal == 0)
					toHour.setValue((toHour.getValue() + 1) % 24);
				else if (oldVal == 0 && newVal == 3)
					toHour.setValue((toHour.getValue() + 23) % 24);
				if (getTimeDiff() < 0) {
					fromHour.setValue(toHour.getValue());
					fromMin.setValue(toMin.getValue());
				}
				calculateBreaks();
			}
		});
		toMin.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
		return v;
	}
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		Dialog dialog = super.onCreateDialog(savedInstanceState);
		dialog.setTitle("Pauze berekening");
		return dialog;
	}
	
	public double getTimeDiff() {
		double start = (double)fromHour.getValue() + (double)fromMin.getValue() / 4;
		double end = (double)toHour.getValue() + (double)toMin.getValue() / 4;
		return end - start;
	}
	
	public void calculateBreaks() {
		double start = (double)fromHour.getValue() + (double)fromMin.getValue() / 4;
		double end = (double)toHour.getValue() + (double)toMin.getValue() / 4;
		List<Break> breaks = Break.getBreaks(start, end);
		ListView breaksView = (ListView) getView().findViewById(R.id.fb_breaks);
		String[] breakStrings = new String[breaks.size()];
		for (int i = 0; i < breaks.size(); i++) {
			breakStrings[i] = getResources().getString(breaks.get(i).getResourceId(), Utils.timeToString(breaks.get(i).getTime()));
		}
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, breakStrings);
		breaksView.setAdapter(adapter);
	}
}
